#!/usr/bin/python
# NMAP Module v0.01
# by William Edwards 07-26-09
# 
# This component is used for gathering user information to execute a network
# scan; that will scan the network for computers using SSH. It then filters
# to the correct Windows or Linux layer to execute the external program.

import layer_lin # Linux Layer for passing Linux-specific commands
import layer_win # Windows Layer for passing Windows-specific commands

# This function prompts the user to enter the base IP address that it will use
# to search for computers.
def get_ip():
	ip = str(raw_input('Enter the base IP address: '))
	return ip

# This function prompts the user to enter the subnet mask of the network to
# search for computers on the network.
def get_subnet():
	# A loop is used so that it will continue to try to get a valid subnet from the user.
	subnet = ''
	valid = False
	while valid != True: # while the subnet is not valid
		subnet = str(raw_input("Enter the subnet (e.g. 255.255.255.0): ")) # Prompts the user.
		subnet, valid = cidr(subnet, valid) # Runs the CIDR convert function to convert the 
		                                    # subnet into CIDR format so it can be passed as 
		                                    # an argument in NMAP.
	return subnet

# Converts the standard readable subnet mask into CIDR format so it can be used in NMAP.
def cidr(subnet, valid):
	# If 255.0.0.0 is chosen, it converts it to CIDR which is 8
	if subnet == "255.0.0.0":
		subnet = "8"
		valid = True
		print "Class A subnets may take a while to scan."
	# If 255.255.0.0 is chosen, it converts it to CIDR which is 16
	elif subnet == "255.255.0.0":
		subnet = "16"
		valid = True
		print "Class B subnets may take a while to scan."
	# If 255.255.255.0 is chosen, it converts it to CIDR which is 24
	elif subnet == "255.255.255.0":
		subnet = "24"
		valid = True
	# Currently if any other subnets are chosen, it will throw an error.
	else:
		print "Please enter a valid subnet."
		subnet = ''
		valid = False
	return subnet, valid

# This will execute the nmap scan to discover devices with SSH open.
def scan(osvar, ip, subnet): # 3 arguments must go with the scan; what os is running, the ip, and subnet
	# Executes the scan and saves the results as a text file.
	print "Scanning... (This may take a while)"
	# Determines if the system is run on Linux or Windows so the correct command can be run.
	if osvar == "Linux":
		layer_lin.nmap_scan(ip, subnet)
	else:
		layer_win.nmap_scan(ip, subnet)
	print "Scanning Complete! Found hosts saved to available_hosts.txt."
	print ''
